package deadlock;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestLiveLock {
    static volatile int counter = 10;
    static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            //期望减到0退出循环
            while (counter > 0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                counter--;
                log.debug("counter = {}", counter);
            }
        },"t1").start();

        new Thread(() -> {
            //期望超过20退出循环
            while (counter > 0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                counter++;
                log.debug("counter = {}", counter);
            }
        },"t2").start();
    }
}
